package com.tengju.bff.interfaces.covercharge.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.tengju.bff.interfaces.covercharge.service.UserService;
import com.tengju.bff.interfaces.covercharge.util.concurrent.ExecutorManager;
import com.tengju.bff.interfaces.covercharge.util.concurrent.TaskAction;
import com.tengju.user.domain.model.user.UserIdCode;
import com.tengju.user.domain.model.user.UserInfo;
import com.tengju.user.domain.model.user.UserInfoRepository;
import com.tengju.user.domain.service.UserDocService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@Slf4j
public class UserServiceImpl implements UserService {

    public static final Integer LIMIT = 300;

    @Autowired
    private UserDocService userDocService;

    /**
     * 查询用户列表
     *
     * @param userIdCodeList 用户ID CODE列表
     * @return 用户列表
     */
    @Override
    public Map<UserIdCode, UserInfo> queryUserMap(List<UserIdCode> userIdCodeList) {
        long time1 = System.currentTimeMillis();
        Map<UserIdCode, UserInfo> userMap = new HashMap<>();

        if (CollectionUtil.isEmpty(userIdCodeList)) {
            return userMap;
        }

        if (userIdCodeList.size() < LIMIT) {
            try {

                List<UserInfo> userInfos = userDocService.searchUserByIdCodes(userIdCodeList);
                Assert.isTrue(userInfos != null, "批量查询用户信息失败");
                for (UserInfo userVO : userInfos) {
                    userMap.put(userVO.getIdCode(), userVO);
                }
            } catch (Exception e) {
                log.error("批量查询用户信息异常", e);
            }
        } else {
             List<TaskAction<List<UserInfo>>> taskActionList = new ArrayList<>();

            for (int i = 0; i < userIdCodeList.size(); i += LIMIT) {
                List<UserIdCode> userIdCodeSubList;
                if (i + LIMIT > userIdCodeList.size()) {
                    userIdCodeSubList = userIdCodeList.subList(i, userIdCodeList.size());
                } else {
                    userIdCodeSubList = userIdCodeList.subList(i, i + LIMIT);
                }

//                try {
//                    UBaseResult<List<UserVO>> uBaseResult = baseServiceFeignClient.getByIdList(userIdSubList);
//                    Assert.isTrue(uBaseResult.isSuccess() && uBaseResult.getData() != null, "批量查询用户信息失败");
//                    for (UserVO userVO : uBaseResult.getData()) {
//                        userMap.put(Integer.parseInt(userVO.getId() + ""), userVO);
//                    }
//                } catch (Exception e) {
//                    log.error("批量查询用户信息异常", e);
//                }

                TaskAction<List<UserInfo>> taskAction = () -> {
                    try {
                        List<UserInfo> userInfos = userDocService.searchUserByIdCodes(userIdCodeList);
                        Assert.isTrue(userInfos != null, "批量查询用户信息失败");
                        return userInfos;
                    } catch (Exception e) {
                        log.error("批量查询用户信息异常", e);
                    }
                    return new ArrayList<>();
                };

                taskActionList.add(taskAction);
            }

            TaskAction[] taskActions = taskActionList.toArray(new TaskAction[0]);
            List<List<UserInfo>> list = ExecutorManager.getInstance().executeTask(taskActions);
            log.info("分页获取用户信息结束，分页数据量：{}", list.size());
            for (List<UserInfo> subList : list) {
                log.info("查询用户信息，每页数据量：{}", subList.size());
                for (UserInfo userVO : subList) {
                    userMap.put(userVO.getIdCode(), userVO);
                }
            }
        }

        long time2 = System.currentTimeMillis();
        log.info("查询用户信息耗时：" + (time2 - time1) + "ms");

        return userMap;
    }
}
